import { Component, OnInit } from '@angular/core'
import { ListBaseClass } from '@shared/classes/ListBaseClass'
import { IRiskControlBiz } from '@shared/interfaces/risk-control'
import { IQueryParams } from '@shared/interfaces/queryParams'
import { NzMessageService } from 'ng-zorro-antd'
import { I18NService } from '@core'
import { Router, ActivatedRoute } from '@angular/router'
import { RiskService } from '../../risk.service'
import { DataService } from '@shared/services/data.service'
import { STColumn } from '@delon/abc'

@Component({
  selector: 'app-risk-detail-tab-levels',
  templateUrl: './tab-levels.component.html',
  styleUrls: ['../../../../shared/styles/table.less'],
})
export class RiskDetailTabLevelsComponent extends ListBaseClass<IRiskControlBiz, IQueryParams> implements OnInit {
  schemeCode: string

  columns: STColumn[] = [
    {
      title: '层级',
      i18n: 'GENERAL.INFO.BIZ_LEVEL',
      index: 'biz',
      width: '150px',
      className: 'text-center',
    },
    {
      title: '层级ID',
      i18n: 'GENERAL.INFO.BIZ_LEVEL_CODE',
      index: 'bizId',
      width: '150px',
      className: 'text-center',
    },
    {
      title: '名称',
      i18n: 'GENERAL.INFO.BIZ_LEVEL_NAME',
      index: 'name',
      width: '150px',
      className: 'text-left',
    },
    {
      title: '英文名称',
      i18n: 'GENERAL.INFO.BIZ_LEVEL_NAME_EN',
      index: 'nameEn',
      width: '150px',
      className: 'text-left',
    },
  ]

  constructor(
    public msg: NzMessageService,
    public i18n: I18NService,
    public router: Router,
    public route: ActivatedRoute,
    private riskService: RiskService,
    private dataService: DataService,
  ) {
    super(msg, i18n, route, router)
  }

  ngOnInit() {
    this.schemeCode = this.dataService.riskControlScheme.schemeCode

    this.parseParams().subscribe(() => {
      this.queryTableDataWithParams()
    })
  }

  queryTableDataWithParams(): void {
    this.loading = true
    this.riskService
      .getBizs(this.schemeCode)
      .subscribe(
        this.renderList.bind(this),
        this.getListError.bind(this),
      )
  }

  renderList(res): void {
    const { data, total } = res
    this.data = data || []
    this.total = total
    this.loading = false
  }

}
